package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.DingTalkBiz;
import com.hst.capacity.domain.common.constants.HashMapConstants;
import com.hst.capacity.domain.model.request.system.SystemAlertParam;
import com.hst.capacity.infra.external.dingtalk.enums.DingMsgType;
import com.hst.capacity.infra.external.dingtalk.model.DingTalkConfig;
import com.hst.capacity.infra.external.dingtalk.model.DingTalkContent;
import com.hst.capacity.infra.external.dingtalk.model.request.order.CancelOrderRemindParam;
import com.hst.capacity.infra.external.dingtalk.model.request.order.NewOrderRemindParam;
import com.hst.capacity.infra.external.dingtalk.service.DingTalkService;
import com.hst.capacity.infra.util.DateUtil;
import com.hst.capacity.infra.util.StringUtil;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DingTalkBizImpl implements DingTalkBiz {

    @Autowired
    DingTalkService dingTalkService;

    @Override
    public void sendNewOrderRemind(NewOrderRemindParam param) throws ApiException {
        DingTalkContent dingTalkContent = new DingTalkContent();
        dingTalkContent.setTitle("新订单提醒");
        dingTalkContent.setContent(this.templateNewOrderRemind(param));

        DingTalkConfig dingTalkConfig = new DingTalkConfig();
        String accessToken = HashMapConstants.DingTalkAccessToken.get(param.getIdFranchisee());
        if (StringUtil.isNotBlank(accessToken))
            dingTalkConfig.setAccessToken(accessToken);
        DingMsgType messageType = DingMsgType.MARKDOWN;
        dingTalkConfig.setMsgType(messageType);

        dingTalkService.sendMessage(dingTalkContent, dingTalkConfig);
    }

    @Override
    public void sendCancelOrderRemind(CancelOrderRemindParam param) throws ApiException {
        DingTalkContent dingTalkContent = new DingTalkContent();
        dingTalkContent.setTitle("乘客取消订单提醒");
        dingTalkContent.setContent(this.templateCancelOrderRemind(param));

        DingTalkConfig dingTalkConfig = new DingTalkConfig();
        String accessToken = HashMapConstants.DingTalkAccessToken.get(param.getIdFranchisee());
        if (StringUtil.isNotBlank(accessToken))
            dingTalkConfig.setAccessToken(accessToken);
        DingMsgType messageType = DingMsgType.MARKDOWN;
        dingTalkConfig.setMsgType(messageType);

        dingTalkService.sendMessage(dingTalkContent, dingTalkConfig);
    }

    @Override
    public void sendSystemAlert(SystemAlertParam param) throws ApiException {
        DingTalkContent dingTalkContent = new DingTalkContent();
        dingTalkContent.setTitle("系统出错提醒");
        dingTalkContent.setContent(this.templateSystemAlert(param));

        DingTalkConfig dingTalkConfig = new DingTalkConfig();
        String accessToken = HashMapConstants.DingTalkAccessToken.get("system-alert");
        if (StringUtil.isNotBlank(accessToken))
            dingTalkConfig.setAccessToken(accessToken);
        DingMsgType messageType = DingMsgType.TEXT;
        dingTalkConfig.setMsgType(messageType);

        dingTalkService.sendMessage(dingTalkContent, dingTalkConfig);
    }

    private static String templateNewOrderRemind(NewOrderRemindParam param) {
        String cityName = param.getCityName();
        if (StringUtil.isBlank(cityName))
            cityName = HashMapConstants.idFranchiseeCityMap.get(param.getIdFranchisee());
        String channel = HashMapConstants.ChannelMap.get(param.getChannel());

        StringBuilder sb=new StringBuilder();
        sb.append("## 新订单提醒 \n");
        sb.append("##### 订单号：");sb.append(param.getIdOrder());sb.append("   \n");
        if (StringUtil.isNotBlank(param.getExternalOrderId())) {
            sb.append("##### 第三方单号：");
            sb.append(param.getExternalOrderId());
            sb.append("   \n");
        }
        if (StringUtil.isNotBlank(cityName)) {
            sb.append("##### 城市：");
            sb.append(cityName);
            sb.append("   \n");
        }
        if (StringUtil.isNotBlank(channel)) {
            sb.append("##### 渠道：");
            sb.append(channel);
            sb.append("   \n");
        }
        sb.append("##### 车型：");sb.append(HashMapConstants.VehicleTypeMap.get(param.getVehicleType()));sb.append("   \n");
        sb.append("##### 用车类型：");sb.append(HashMapConstants.CarpoolTypeMap.get(param.getCarpoolType()));sb.append("   \n");
        if (StringUtil.isNotBlank(param.getFlightNo())) {
            sb.append("##### 航班号：");
            sb.append(param.getFlightNo());
            sb.append("   \n");
        }
        if (param.getOrderMoney() != null) {
            sb.append("##### 金额：");
            sb.append(param.getOrderMoney());
            sb.append("元");
            sb.append("   \n");
        }
        sb.append("##### 乘客电话：");sb.append(param.getPassengerMobile());sb.append("   \n");
        sb.append("##### 用车时间：");sb.append(DateUtil.formatDateTime(param.getAppointmentTime()));sb.append("   \n");
        sb.append("##### 出发地点：");sb.append(param.getStartAddress());sb.append("   \n");
        sb.append("##### 到达地点：");sb.append(param.getEndAddress());

        return sb.toString();
    }

    private static String templateCancelOrderRemind(CancelOrderRemindParam param) {
        //测试数据
        String cityName = param.getCityName();
        if (StringUtil.isBlank(cityName))
            cityName = HashMapConstants.idFranchiseeCityMap.get(param.getIdFranchisee());
        String channel = HashMapConstants.ChannelMap.get(param.getChannel());

        StringBuilder sb=new StringBuilder();
        sb.append("## 取消订单提醒 \n");
        sb.append("##### 订单号：");sb.append(param.getIdOrder());sb.append("   \n");
        if (StringUtil.isNotBlank(param.getExternalOrderId())) {
            sb.append("##### 第三方单号：");
            sb.append(param.getExternalOrderId());
            sb.append("   \n");
        }
        if (StringUtil.isNotBlank(cityName)) {
            sb.append("##### 城市：");
            sb.append(cityName);
            sb.append("   \n");
        }
        if (StringUtil.isNotBlank(channel)) {
            sb.append("##### 渠道：");
            sb.append(channel);
            sb.append("   \n");
        }
        sb.append("##### 车型：");sb.append(HashMapConstants.VehicleTypeMap.get(param.getVehicleType()));sb.append("   \n");
        sb.append("##### 用车类型：");sb.append(HashMapConstants.CarpoolTypeMap.get(param.getCarpoolType()));sb.append("   \n");
        if (StringUtil.isNotBlank(param.getFlightNo())) {
            sb.append("##### 航班号：");
            sb.append(param.getFlightNo());
            sb.append("   \n");
        }
        if (param.getOrderMoney() != null) {
            sb.append("##### 金额：");
            sb.append(param.getOrderMoney());
            sb.append("元");
            sb.append("   \n");
        }
        sb.append("##### 乘客电话：");sb.append(param.getPassengerMobile());sb.append("   \n");
        sb.append("##### 用车时间：");sb.append(DateUtil.formatDateTime(param.getAppointmentTime()));sb.append("   \n");
        if (StringUtil.isNotBlank(param.getDriverInfo())) {
            sb.append("##### 司机信息：");
            sb.append(param.getDriverInfo());
            sb.append("   \n");
        }
        sb.append("##### 出发地点：");sb.append(param.getStartAddress());sb.append("   \n");
        sb.append("##### 到达地点：");sb.append(param.getEndAddress());

        return sb.toString();
    }

    private static String templateSystemAlert(SystemAlertParam param) {
        StringBuilder sb=new StringBuilder();
        sb.append("## 系统错误提醒 \n");
        sb.append(param.getMessage());
        return sb.toString();
    }

}
